package com.zang.blogz.controller;

import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.entity.Role;
import com.zang.blogz.entity.RoleMenu;
import com.zang.blogz.entity.RoleResource;
import com.zang.blogz.model.input.ro.ResourceRO;
import com.zang.blogz.model.input.ro.RoleMenuRO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/blogz/role")
@Api(tags = "角色模块")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/admin/updateResource")
    @ApiOperation(value = "更新或新增角色所拥有的资源",notes = "更新或新增角色所拥有的资源")
    @OptLog(optType = OptTypeConstant.SAVE_OR_UPDATE)
    public Result<List<RoleResource>> updateResource(@RequestBody ResourceRO resourceRo) {

        return Result.ok(roleService.updateResource(resourceRo.getRoleId(), resourceRo.getResourceIds()));
    }

    @PostMapping("/admin/updateMenu")
    @ApiOperation(value = "更新或新增角色所拥有的菜单", notes = "更新或新增角色所拥有的菜单")
    @OptLog(optType = OptTypeConstant.SAVE_OR_UPDATE)
    public Result<List<RoleMenu>> updateMenu(@RequestBody RoleMenuRO roleMenuRO){

        return Result.ok(roleService.updateMenu(roleMenuRO.getRoleId(), roleMenuRO.getMenuIds()));
    }

    @PostMapping("/admin/saveOrUpdateRole")
    @ApiOperation(value = "新增或编辑角色", notes = "新增或编辑角色")
    @OptLog(optType = OptTypeConstant.SAVE_OR_UPDATE)
    public Result<Role> saveRole(@RequestBody Role role){

        return roleService.saveOrUpdateRole(role);
    }

    @DeleteMapping("/admin/deleteRole")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @OptLog(optType = OptTypeConstant.REMOVE)
    public Result<Boolean> deleteRole(@RequestParam(value = "id") Integer id){

        return roleService.deleteRole(id);
    }

}
